package cn.tedu.charging.order.mqtt;

import com.sun.xml.internal.ws.policy.EffectiveAlternativeSelector;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.hamcrest.Condition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * EMQX的消息中间的发送者
 * 订单服务 给 设备发送 消息(指令)
 */
@Component
@Slf4j
public class MqttProducer {
    @Autowired
    private MqttClient mqttClient;
    /**
     * 通过MqttConfiguration类中 @Bean注解创建的额 MqttClient
     * 给EMQX消息中间件
     * 发送消息
     * @param message
     */
   /* public void send(String topic,String message) throws MqttException {
        mqttClient.publish(topic,message.getBytes(),0,true);
    }*/
    public  Boolean send(String topic,String message){
        Boolean succenss = false;
        try {
            mqttClient.publish(topic,message.getBytes(),0,true);
            succenss=true;
        } catch (MqttException e) {
            log.error("给EMQX发送消息失败,topic:{},消息:{}",topic,message,e);
        }
        return succenss;
    }
    /* public void send(String topic,String message)  {
        try {
            mqttClient.publish(topic,message.getBytes(),0,true);
        } catch (MqttException e) {
            //捕获了异常，不处理，把异常吃掉了
        }
    }*/
}
